package com.iotechn.unimall.admin.api.store;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.core.exception.ServiceExceptionDefinition;
import com.iotechn.unimall.data.domain.*;
import com.iotechn.unimall.data.mapper.*;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.SessionUtil;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class AdminStoreServiceImpl implements AdminStoreService {
    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<StoreDO> list(Integer page, Integer limit, Long adminId, String storeName, String gmtStartCreate, String gmtEndCreate, String status) throws ServiceException {
        Wrapper<StoreDO> wrapper = new EntityWrapper<StoreDO>();
        //名称搜索
        if (!StringUtils.isEmpty(storeName)) {
            wrapper.like("store_name", storeName);
        }
        //状态搜索
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        //时间范围搜索
        if (!StringUtils.isEmpty(gmtStartCreate)) {
            wrapper.gt("gmt_create", gmtStartCreate);
        }
        if (!StringUtils.isEmpty(gmtEndCreate)) {
            wrapper.le("gmt_create", gmtEndCreate);
        }

        wrapper.orderBy("id", false);
        Integer count = storeMapper.selectCount(wrapper);
        List<StoreDO> StoreDOS = storeMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
       //经纬度为null时设置为""
        StoreDOS =StoreDOS.stream().map(i->{
            i.setStoreLalg(i.getStoreLalg()==null?"":i.getStoreLalg());
            return i;
        }).collect(Collectors.toList());
        return new Page<>(StoreDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StoreDO create(StoreDO storeDO, Long adminId) throws ServiceException {
        storeDO.setAdminId(adminId);
        Date now = new Date();
        storeDO.setGmtCreate(now);
        storeDO.setGmtUpdate(now);
        storeDO.setStatus(1);//创建时状态为激活
        storeDO.setLimitRate(0);
        if(StringUtils.isNotEmpty(storeDO.getStoreLg()) &&  StringUtils.isNotEmpty(storeDO.getStoreLa())){
            storeDO.setStoreLalg(storeDO.getStoreLg()+","+storeDO.getStoreLa());
        }
        //时间
        if (!StringUtils.isEmpty(storeDO.getStoreStartTime()) && !StringUtils.isEmpty(storeDO.getStoreEndTime())) {
            storeDO.setStoreTime(storeDO.getStoreStartTime() + "-" + storeDO.getStoreEndTime());
        }

        if (storeMapper.insert(storeDO) > 0) {
            storeDO.setId(storeDO.getId());
             return storeDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(StoreDO storeDO, Long adminId) throws ServiceException {
        Long id = storeDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        storeDO.setGmtUpdate(now);

        //时间
        if (!StringUtils.isEmpty(storeDO.getStoreStartTime()) && !StringUtils.isEmpty(storeDO.getStoreEndTime())) {
            storeDO.setStoreTime(storeDO.getStoreStartTime() + "-" + storeDO.getStoreEndTime());
        }

        if (storeMapper.updateById(storeDO) > 0) {

            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (storeMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    public StoreDO detail(Long id, Long adminId) throws ServiceException {
        StoreDO storeDO = storeMapper.selectById(id);
        if (!StringUtils.isEmpty(storeDO.getStoreTime())) {
            String[] timeRange = storeDO.getStoreTime().split("-");
            storeDO.setStoreStartTime(timeRange[0]);
            storeDO.setStoreEndTime(timeRange[1]);
        }
        return storeDO;
    }

    @Override
    public String updateStatus(Long id, Integer status, Long adminId) throws ServiceException {
        StoreDO storeDO = storeMapper.selectById(id);
        if (storeDO == null) {
            return "fail";
        }
        storeDO.setStatus(status);//冻结
        Date now = new Date();
        storeDO.setGmtUpdate(now);
        storeDO.setAdminId(adminId);
        if (storeMapper.updateById(storeDO) > 0) {//删除改为假删除，更改状态为0
            return "ok";
        }
        return "fail";
    }

    @Override
    public List<StoreDO> listStoresByPara(String status,Long adminId) throws ServiceException {
        Long id = SessionUtil.getAdmin().getId();
        Wrapper<StoreDO> storeDOEntityWrapper = new EntityWrapper<>();
        storeDOEntityWrapper.eq(!StringUtils.isEmpty(status), "status", status);
        List<StoreDO> storeDOS = storeMapper.selectList(storeDOEntityWrapper);
        return storeDOS;
    }



    @Override
    public List<StoreDO> listStore(String status,Long adminId) throws ServiceException {
        Long id = SessionUtil.getAdmin().getId();
        Wrapper<StoreDO> storeDOEntityWrapper = new EntityWrapper<>();
        storeDOEntityWrapper.eq(!StringUtils.isEmpty(status), "status", status);
        List<StoreDO> storeDOS = storeMapper.selectList(storeDOEntityWrapper);
        return storeDOS;
    }
}